<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Statement Account</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<link href="css/test/TestStatement.css" rel="stylesheet" type="text/css"/>
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<script type="text/javascript">
	function storeSended(){
		
		var postData = document.getElementById("storeData");
		jQuery.ajax({type:'post', async:false, url:'store_message.php', data:postData, success:function(result){
			}
		});
	}




	function printStatement(){
		document.getElementById("header").style.display = "none";
		
		document.getElementById("headerTitle").style.display = "none";
		//document.getElementById("main_content").style.display = "none";
		document.getElementById("nav").style.display = "none";
		document.getElementById("statementOuter").style.display = "none";
		document.getElementById("footer").style.display = "none";
		document.getElementById("data").style.overflow = "visible";
		document.getElementById("footer").style.display = "none";
		//document.getElementById("statementInfo").style.display = "block";
		//document.getElementById("footer").style.display = "none";
		//document.getElementById("footer").style.display = "none";
		//change 
		document.getElementById("statementHead").style.display = "none";
		document.getElementById("addressPrint").innerHTML = jQuery("#fromAddress").text();
		document.getElementById("landlinePrint").innerHTML = document.getElementById("landline").value;
		document.getElementById("statementHeadPrint").style.display = "block";
		window.print();

		//document.getElementById("main_content").style.display = "block";
		document.getElementById("data").style.overflow = "auto";
		document.getElementById("headerTitle").style.display = "block";
		document.getElementById("statementHead").style.display = "block";
		document.getElementById("statementHeadPrint").style.display = "none";
		document.getElementById("header").style.display = "block";
		document.getElementById("nav").style.display = "block";
		document.getElementById("statementOuter").style.display = "block";
		document.getElementById("footer").style.display = "block";
	}



	
	function previousPage(){
		var lastURL = document.referrer;
		if( lastURL == "" ){
			window.location.href = "index.php";
			return;
		}
		var lastPage = lastURL.substring(lastURL.lastIndexOf("/") + 1);
		window.location.href = lastPage;
	}
</script>
</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini", "rw");
			
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			
			if( !isset($_GET["patientNo"])){
				echo "<script>alert('no patient find!!!');window.location.href='index.php';</script>";
			}
			$patientNo = mysqli_real_escape_string($conn,$_GET["patientNo"]);
		
	
			
	
			
			//patient info
			$getPatientInfoSql = "select First_name, Surname, address from patient where Patient_no=".$patientNo;
			$patientResult = mysqli_query($conn, $getPatientInfoSql);
			$patientFirstName = "";
			$patientSurname = "";
			$patientAddress = "";
			if( $row = mysqli_fetch_array($patientResult) ){
				$patientFirstName = $row["First_name"];
				$patientSurname = $row["Surname"];
				$patientAddress = $row["address"];
			}else{
				echo "<script>alert('this patient not exist!!');window.location.href='index.php';</script>";
			}
			
			date_default_timezone_set($ini_array["default_timezone"]);
			//account time
			$getTimeSql = "select max(Datetime_patient_seen) max, min(Datetime_patient_seen) min from visit where Patient_no=".$patientNo;
			$timeResult = mysqli_query($conn, $getTimeSql);
			$maxTime = "";
			$minTime = "";
			if( $row = mysqli_fetch_array($timeResult) ){
				$maxTime = date("d/m/Y",strtotime($row["max"]));
				$minTime = date("d/m/Y",strtotime($row["min"]));
			}else{
				$maxTime = date("d/m/Y");
				$minTime = date("d/m/Y");
			}
			
			//account info
			$getAccountInfoSql = "select Appointment_no, Datetime_patient_seen, Professional_fee, payment_method from visit where Patient_no=".$patientNo;
			$accountResult = mysqli_query($conn, $getAccountInfoSql);
			
			
			
			
			
		?>

		<div id="main_content" style="width:610px;margin:10px auto;">
			<div id="statementInfo" style="border:solid 1px black;width:600px;margin:80px auto;height:600px;">
				<div id="statementHead" style="width:800px;margin:10px auto;">
					<table style="width:780px;">
						<tr>
							<td style="width:40%;text-align:left;">
							<div id="upleftPart">
								<div><span style="font-weight:bold;">Dr. Geraldine Mbrrow</span></div>
								<div><label> M.B., B.Ch., B.A.O., M.R.C.P.I.</label></div>
								<div><span style="font-weight:bold;">Consultant  Dermatologist.</span></div>
							</div>
							</td>
							<td style="width:20%;">
							<div id="uprightPart">
								<textarea id="fromAddress" name="fromAddress" style="resize:none;"><?php echo $ini_array["fromAddress"]; ?></textarea>
							</div>
							</td >
							<td style="width:40%"><input type="text" name="landline" id="landline" style="width:100px;" value="<?php echo $ini_array["landline"];?>"/></td>
						</tr>
					</table>
				</div>
				<div id="statementHeadPrint" style="width:800px;margin:10px auto;display:none">
					<table  style="width:780px;">
						<tr>
							<td style="width:40%;text-align:left;">
							<div id="upleftPart">
								<div><span style="font-weight:bold;">Dr. Geraldine Mbrrow</span></div>
								<div><label> M.B., B.Ch., B.A.O., M.R.C.P.I.</label></div>
								<div><span style="font-weight:bold;">Consultant  Dermatologist.</span></div>
							</div>
							</td>
							<td  style="width:20%;">
							<div id="uprightPart">
								<p id="addressPrint"></p>
							</div>
							</td>
							<td  style="width:40%"><label id="landlinePrint" style="width:100px;"></label></td>
						</tr>
					</table>
				</div>
				<div id="contentTitle" style="position:relative;left:0px;">
					<h1>Statement Account</h1>
					<div id="timeDiv">
						<table>
							<tr>
								<td><label>Period Covered</label></td>
								<td><label>From:</label></td>
								<td><label><?php echo $minTime?></label></td>
							</tr>
							<tr>
								<td></td>
								<td><label>To:</label></td>
								<td><label><?php echo $maxTime?></label></td>
							</tr>
						</table>
					</div>
				</div>
				
	
				<div id="patientPart" style="margin:10px auto;width:800px;">
					<table>
						<tr>
							<td style="text-align:right;"><label>Patient No.:</label></td>
							<td><input type="text" name="patientNo" id="patientNo" readonly="readonly"  maxlength="20" size="30" value="<?php echo $patientNo?>"/></td>
							<td></td>
							<td></td>
						</tr>
						<tr>
							<td style="text-align:right;"><label>First Name:</label></td>
							<td><input type="text" name="firstName" id="firstName" readonly="readonly" value="<?php echo $patientFirstName?>"/></td>
							<td><label>Address:</label></td>
							<td rowspan="2"><textarea name="address" id="address" readonly="readonly" style="resize:none"><?php echo $patientAddress?></textarea></td>
						</tr>
						<tr>
							<td style="text-align:right;"><label>Surname:</label></td>
							<td><input type="text" name="surname" id="surname" readonly="readonly" value="<?php echo $patientSurname?>"/></td>
							<td></td>

						</tr>
					</table>
				
					
				</div>

				<div style="clear:both;"></div>
				<div id="statementData" style="margin:10px auto;width:800px;">
					<div style="width:200px;display:inline-block;text-align:center;"><label class="firstCol">date Patient Seen</label></div>
					<div style="width:200px;display:inline-block;text-align:center;"><label class="secondCol">Professional Fee</label></div>
					<div style="width:200px;display:inline-block;text-align:center;"><label class="thirdCol">Payment Method</label></div>
					<div id="data" style="overflow:auto;heigth:400px;width:600px;">
						<table style="width:600px;">
						<?php $content = "{\\\"from\":\"".$minTime."\", \"to\":\"".$maxTime."\",\"data\":[";
							$i = 0;
							while( $row = mysqli_fetch_array($accountResult) ){
								$appointmentNo = $row["Appointment_no"];
								$datetime = $row["Datetime_patient_seen"];
								$fee = $row["Professional_fee"];
								$method = $row["payment_method"];
								if( $i == 0 ){	
									$content .= "{\"datetime\":\"".date("d/m/Y H:i",strtotime($datetime))."\",";
									$content .= "\"fee\":\"".$fee."\",";
									$content .= "\"method\":\"".$method."\"}";
								}else{
									$content .= ",{\"datetime\":\"".date("d/m/Y H:i",strtotime($datetime))."\",";
									$content .= "\"fee\":\"".$fee."\",";
									$content .= "\"method\":\"".$method."\"}";
								}
								$i++;
						?>
						<tr>
							<td style="width:33%;text-align:center;"><label class="firstCol"><?php echo $datetime?></label></td>
							<td style="width:33%;text-align:center;"><label class="secondCol"><?php echo $fee?></label></td>
							<td style="width:33%;text-align:center;"><label class="thirdCol"><?php echo $method?></label></td>
						</tr>
						<?php 
							}
							$content .= "]}";
							$data = "objectNo=".$patientNo."&state=1&type=4&content=".$content."";
							
						?>
						</table>
						
					</div>
					
				</div> 
			</div>
			<div id="statementOuter" style="text-align:center;">
				<form action="store_message.php" method="post" target="hidden_frame">
					<input type="hidden" value="<?php echo $patientNo?>" name="objectNo" />
					<input type="hidden" value="1" name="state" />
					<input type="hidden" value="4" name="type" />
					<input type="hidden" value='<?php echo $content?>' id="content" name="content"/>
					<input type="button" value="Print" onclick="printStatement()"/>
					<input type="button" value="return" onclick="previousPage()" />
					<iframe name="hidden_frame" id="hidden_frame" style="display:none"></iframe>
				</form> 
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
